package com.ziytek.web.citizen.service;

import cn.idev.excel.FastExcel;
import com.ziytek.web.citizen.model.req.CapStatusQueryReq;
import com.ziytek.web.citizen.model.rsp.CapStatusQueryRsp;
import com.ziytek.web.citizen.model.vo.CapStatusVo;
import com.ziytek.web.citizen.model.vo.CapacityStatusExportVo;
import com.ziytek.web.citizen.util.DateTimeUtils;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 容量数据导出
 *
 * @author fenghx
 */
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class CapacityStatusExportService {
    private final CapacityStatusService capacityStatusService;

    public void export(CapStatusQueryReq req, HttpServletResponse response) throws IOException {
        req.setPageNum(1);
        req.setPageSize(100000000);
        CapStatusQueryRsp rsp;
        if (StringUtils.isNotBlank(req.getBatchNo()) && DateTimeUtils.isToday(req.getBatchNo())) {
            rsp = capacityStatusService.capacityStatusQuery(req);
        } else {
            rsp = capacityStatusService.historyCapacityStatusQuery(req);
        }

        List<CapacityStatusExportVo> dataList = new ArrayList<>();
        if (rsp != null && !CollectionUtils.isEmpty(rsp.getData())) {
            dataList = getDataList(rsp.getData());
        }

        // 设置响应头
        var name = "电流容量数据_" + req.getBatchNo()
                .replaceAll("-", "")
                .replaceAll(":", "")
                .replaceAll(" ", "")
                + ".xlsx";

        response.reset();
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + URLEncoder.encode(name, StandardCharsets.UTF_8));
        response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/zip; charset=utf-8");

        // 使用 FastExcel 导出
        FastExcel.write(response.getOutputStream())
                .sheet("电流容量数据")
                .head(CapacityStatusExportVo.class)
                .doWrite(dataList);
    }


    private List<CapacityStatusExportVo> getDataList(List<CapStatusVo> vo) {
        if (CollectionUtils.isEmpty(vo)) {
            return Collections.emptyList();
        }

        List<CapacityStatusExportVo> dataList = new ArrayList<>();
        for (var cv : vo) {
            if (cv == null) {
                continue;
            }

            var cve = new CapacityStatusExportVo();
            cve.setChannelNo(cv.getChannelNo());
            cve.setBatteryNo(cv.getBatteryNo());
            var capStr = String.format("%.2f", cv.getCapacity());
            cve.setCapacity(capStr + "mAh");
            cve.setTestTime(DateTimeUtils.toDate(cv.getExecutionTime(), null));
            dataList.add(cve);
        }

        return dataList;
    }
}
